package com.digitalbuana.smiles.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.digitalbuana.smiles.R;
import com.digitalbuana.smiles.data.Application;
import com.digitalbuana.smiles.data.NetworkException;
import com.digitalbuana.smiles.data.account.AccountManager;
import com.digitalbuana.smiles.data.intent.AccountIntentBuilder;
import com.digitalbuana.smiles.ui.helper.BaseUIActivity;
import com.digitalbuana.smiles.ui.helper.ManagedActivity;
import com.digitalbuana.smiles.utils.FontUtils;

public class LoginActivity extends BaseUIActivity implements
View.OnClickListener {
	
	private ScrollView scrollView;
	private EditText editUsername;
	private EditText editPassword;
	private FrameLayout btnForgot;
	private FrameLayout btnLogin;
	private TextView warningMessage;
	
	private static String tempUsername="";
	
	private Animation shake;
	private Animation fadein;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (isFinishing())
			return;
		
		setContentView(R.layout.activity_login);
		//Casting
		rootView = (FrameLayout)findViewById(R.id.loginRootView);
		FontUtils.setNormalRobotoFont(context, rootView);
		
		scrollView = (ScrollView)findViewById(R.id.loginScrollView);
		editUsername = (EditText)findViewById(R.id.loginEditUsername);
		editPassword = (EditText)findViewById(R.id.loginEditPassword);
		btnForgot = (FrameLayout)findViewById(R.id.loginBtnForget);
		btnLogin = (FrameLayout)findViewById(R.id.loginBtnLogin);
		warningMessage = (TextView)findViewById(R.id.loginWarningMessage);
		
		shake = AnimationUtils.loadAnimation(this, R.anim.shake);
		fadein = AnimationUtils.loadAnimation(this, R.anim.fadein);
		
		//SettingListener
		btnForgot.setOnClickListener(this);
		btnLogin.setOnClickListener(this);
		
		warningMessage.setVisibility(View.GONE);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		editUsername.setText(tempUsername);
		scrollView.startAnimation(fadein);
	}
	
	@Override
	protected void resettingView() {
		super.resettingView();
		if(viewWidth>=500){
			scrollView.getLayoutParams().width=480;
		}
	}
	
	private boolean checkForm()
	{
	    if (editUsername.getText().length()<=4) {
	    	editUsername.startAnimation(shake);
			warningMessage.setVisibility(View.VISIBLE);
			String falied = getResources().getString(R.string.stringFailedID);
			warningMessage.setText(falied);
			editUsername.requestFocus();
			return false;
	    }
		if(editPassword.getText().length()<=4){
			editPassword.startAnimation(shake);
			warningMessage.setVisibility(View.VISIBLE);
			String falied = getResources().getString(R.string.stringFailedPassword);
			warningMessage.setText(falied);
			editPassword.requestFocus();
			return false;
		}
		return true;
	}
	
	private void doForgetPassword(){
		scrollView.startAnimation(shake);
	}
	private void doLogin(){
		if(checkForm()){
//			gagalLogin();
			suksesLogin();
		}
	}
	private void suksesLogin(){
		String account;
		try {
			account = AccountManager.getInstance().addAccount(editUsername.getText().toString(), editPassword.getText().toString());
		} catch (NetworkException e) {
			Application.getInstance().onError(e);
			return;
		}
		setResult(RESULT_OK, createAuthenticatorResult(this, account));
		finish();
	}

	private void gagalLogin(){
		warningMessage.setVisibility(View.VISIBLE);
		String falied = getResources().getString(R.string.stringFailedLogin);
		warningMessage.setText(falied);
		warningMessage.startAnimation(shake);
		warningMessage.requestFocus();
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.loginBtnForget:doForgetPassword();break;
		case R.id.loginBtnLogin:doLogin();break;
		}
	}

	public static Intent createIntent(Context context) {
		return new Intent(context, LoginActivity.class);
	}

	private static Intent createAuthenticatorResult(Context context, String account) {
		return new AccountIntentBuilder(null, null).setAccount(account).build();
	}

	public static String getAuthenticatorResultAccount(Intent intent) {
		return AccountIntentBuilder.getAccount(intent);
	}
}
